package com.mmall.controller;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mmall.beans.PageQuery;
import com.mmall.beans.PageResult;
import com.mmall.common.JsonData;
import com.mmall.common.dtree.DTree;
import com.mmall.common.dtree.DTreeResponse;
import com.mmall.common.dtree.Status;
import com.mmall.model.SysRole;
import com.mmall.param.RoleParam;
import com.mmall.service.SysRoleAclService;
import com.mmall.service.SysRoleService;
import com.mmall.service.SysRoleUserService;
import com.mmall.service.SysTreeService;
import com.mmall.service.SysUserService;

@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleAclService sysRoleAclService;
    @Resource
    private SysRoleUserService sysRoleUserService;
    @Resource
    private SysUserService sysUserService;

    @RequestMapping("/role.page")
    public ModelAndView page() {
        return new ModelAndView("/sys/role/roleList");
    }
    
    @RequestMapping("/edit.page")
    public ModelAndView editPage() {
        return new ModelAndView("/sys/role/roleEdit");
    }
    
    @RequestMapping("/card.page")
    public ModelAndView roleCardPage(@RequestParam(value="id") String id,@RequestParam(value="openPage") String openPage) {
    	HashMap<String, String> map = new HashMap<String, String>();
        map.put("id", id);
        return new ModelAndView(openPage,map);
    }
    
    @RequestMapping("/tree.json")
    @ResponseBody
    public DTreeResponse tree(@RequestParam("id") String roled) {
    	DTreeResponse resp = new DTreeResponse();
    	List<DTree> dtoList = sysTreeService.roleTree();
    	resp.setStatus(new Status());
    	resp.setData(dtoList);
    	return resp;
    }
    
    @RequestMapping("/roleUserTree.json")
    @ResponseBody
    public DTreeResponse roleUserTree(@RequestParam("id") String roleId) {
    	DTreeResponse resp = new DTreeResponse();
    	List<DTree> dtoList = sysTreeService.roleUserTree(roleId);
    	resp.setStatus(new Status());
    	resp.setData(dtoList);
    	return resp;
    }
    
    
    @RequestMapping("/page.json")
	@ResponseBody
	public JsonData queryList(PageQuery pageQuery,SysRole sysRole){
		PageResult<SysRole> result = sysRoleService.getPage(pageQuery,sysRole);
        return JsonData.success(result);
	}

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveRole(RoleParam param) {
        sysRoleService.save(param);
        return JsonData.success(null,"角色新增成功");
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateRole(RoleParam param) {
        sysRoleService.update(param);
        return JsonData.success(null,"角色修改成功");
    }
    
    @RequestMapping("/delete.json")
	@ResponseBody
	public JsonData deleteRole(@RequestParam("id") String roleId){
    	sysRoleService.delete(roleId);
		return JsonData.success(null,"删除角色信息成功");
	}
    
    @RequestMapping("/details.json")
	@ResponseBody
	public JsonData detailsRole(@RequestParam("id") String roleId){
    	SysRole sysRole = sysRoleService.selectByPrimaryKey(roleId);
		return JsonData.success(sysRole);
	}

}
